﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Entidades.Sistema.Distribucion;
using Entidades.Sistema.Venta;
using Mapping.Sistema.Venta;

namespace Mapping.Sistema.Distribucion
{
    public class MappingVehiculo
    {
        public static List<Vehiculo> ConsultarVehiculos()
        {
            try
            {
                SqlCommand cmdVehiculo = new SqlCommand();
                cmdVehiculo.CommandText = "sp_ConsultarVehiculos";
                cmdVehiculo.CommandType = System.Data.CommandType.StoredProcedure;
                Servicios.Conexion.ObtenerInstancia().Conectar("Sistema");
                cmdVehiculo.Connection = Servicios.Conexion.ObtenerInstancia().RetornarConexion();
                SqlDataReader drVehiculo = cmdVehiculo.ExecuteReader();

                List<Vehiculo> ColVehiculoes = new List<Vehiculo>();

                while (drVehiculo.Read())
                {
                    Vehiculo oVehiculo = new Vehiculo();
                    oVehiculo.IdVehiculo = Convert.ToInt32(drVehiculo["Id_Vehiculo"]);
                    oVehiculo.Marca = drVehiculo["Marca"].ToString();
                    oVehiculo.Modelo = drVehiculo["Modelo"].ToString();
                    oVehiculo.Dominio = drVehiculo["Dominio"].ToString();
                    oVehiculo.FechaAdquisicion = Convert.ToDateTime(drVehiculo["FechaAdquisicion"]);
                    oVehiculo.TipoServicioDespacho = (Entidades.Sistema.Distribucion.CoR.EnumTipoServicioDespacho)Convert.ToInt32(drVehiculo["TipoServicioDespacho"]);
                    ColVehiculoes.Add(oVehiculo);
                }

                Servicios.Conexion.ObtenerInstancia().Desconectar("Sistema");
                return ColVehiculoes;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static Vehiculo ConsultarVehiculoPorClave(int idVehiculo)
        {
            try
            {
                SqlCommand cmdVehiculo = new SqlCommand();
                cmdVehiculo.CommandText = "sp_ConsultarVehiculoPorClave";
                cmdVehiculo.CommandType = System.Data.CommandType.StoredProcedure;
                cmdVehiculo.Connection = Servicios.Conexion.ObtenerInstancia().RetornarConexion();
                cmdVehiculo.Parameters.Add("@Id_Vehiculo", System.Data.SqlDbType.Int).Value = idVehiculo;
                SqlDataReader drVehiculo = cmdVehiculo.ExecuteReader();

                Vehiculo oVehiculo = new Vehiculo();

                while (drVehiculo.Read())
                {
                    oVehiculo.IdVehiculo = Convert.ToInt32(drVehiculo["Id_Vehiculo"]);
                    oVehiculo.Marca = drVehiculo["Marca"].ToString();
                    oVehiculo.Modelo = drVehiculo["Modelo"].ToString();
                    oVehiculo.Dominio = drVehiculo["Dominio"].ToString();
                    oVehiculo.FechaAdquisicion = Convert.ToDateTime(drVehiculo["FechaAdquisicion"]);                    
                    oVehiculo.TipoServicioDespacho = (Entidades.Sistema.Distribucion.CoR.EnumTipoServicioDespacho)Convert.ToInt32(drVehiculo["TipoServicioDespacho"]);
                }

                return oVehiculo;
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        public static bool AgregarVehiculo(Vehiculo oVehiculo)
        {
            SqlCommand cmdVehiculo = new SqlCommand();
            cmdVehiculo.CommandText = "sp_AgregarVehiculo";
            cmdVehiculo.CommandType = System.Data.CommandType.StoredProcedure;
            Servicios.Conexion.ObtenerInstancia().Conectar("Sistema");
            cmdVehiculo.Connection = Servicios.Conexion.ObtenerInstancia().RetornarConexion();
            cmdVehiculo.Parameters.Add("@Id_Vehiculo", System.Data.SqlDbType.Int).Value = oVehiculo.IdVehiculo;
            cmdVehiculo.Parameters.Add("@Marca", System.Data.SqlDbType.VarChar, 50).Value = oVehiculo.Marca;
            cmdVehiculo.Parameters.Add("@Modelo", System.Data.SqlDbType.VarChar, 50).Value = oVehiculo.Modelo;
            cmdVehiculo.Parameters.Add("@Dominio", System.Data.SqlDbType.VarChar, 50).Value = oVehiculo.Dominio;
            cmdVehiculo.Parameters.Add("@FechaAdquisicion", System.Data.SqlDbType.Date).Value = oVehiculo.FechaAdquisicion;
            cmdVehiculo.Parameters.Add("@TipoServicioDespacho", System.Data.SqlDbType.Int).Value = (int)oVehiculo.TipoServicioDespacho;
            cmdVehiculo.ExecuteNonQuery();
            Servicios.Conexion.ObtenerInstancia().Desconectar("Sistema");
            return true;
        }

        public static int ModificarVehiculo(Vehiculo oVehiculo)
        {
            SqlCommand cmdVehiculo = new SqlCommand();
            cmdVehiculo.CommandText = "sp_ModificarVehiculo";
            cmdVehiculo.CommandType = System.Data.CommandType.StoredProcedure;
            Servicios.Conexion.ObtenerInstancia().Conectar("Sistema");
            cmdVehiculo.Connection = Servicios.Conexion.ObtenerInstancia().RetornarConexion();
            cmdVehiculo.Parameters.Add("@Id_Vehiculo", System.Data.SqlDbType.Int).Value = oVehiculo.IdVehiculo;
            cmdVehiculo.Parameters.Add("@Marca", System.Data.SqlDbType.VarChar, 50).Value = oVehiculo.Marca;
            cmdVehiculo.Parameters.Add("@Modelo", System.Data.SqlDbType.VarChar, 50).Value = oVehiculo.Modelo;
            cmdVehiculo.Parameters.Add("@Dominio", System.Data.SqlDbType.VarChar, 50).Value = oVehiculo.Dominio;
            cmdVehiculo.Parameters.Add("@FechaAdquisicion", System.Data.SqlDbType.Date).Value = oVehiculo.FechaAdquisicion;
            cmdVehiculo.Parameters.Add("@TipoServicioDespacho", System.Data.SqlDbType.Int).Value = (int)oVehiculo.TipoServicioDespacho;
            int nro = Convert.ToInt32(cmdVehiculo.ExecuteScalar());

            Servicios.Conexion.ObtenerInstancia().Desconectar("Sistema");
            return nro;
        }

        public static bool EliminarVehiculo(Vehiculo oVehiculo)
        {
            SqlCommand cmdEliminarVehiculo = new SqlCommand();
            cmdEliminarVehiculo.CommandText = "sp_EliminarVehiculo";
            Servicios.Conexion.ObtenerInstancia().Conectar("Sistema");
            cmdEliminarVehiculo.CommandType = System.Data.CommandType.StoredProcedure;
            cmdEliminarVehiculo.Connection = Servicios.Conexion.ObtenerInstancia().RetornarConexion();
            cmdEliminarVehiculo.Parameters.Add("@Id_Vehiculo", System.Data.SqlDbType.Int).Value = oVehiculo.IdVehiculo;
            cmdEliminarVehiculo.ExecuteNonQuery();
            Servicios.Conexion.ObtenerInstancia().Desconectar("Sistema");
            
            return true;
        }
    }
}
